<?php 
/**
 * Javascript support file for reference filter.
 * Uses php so we can get path to local yui libs 
 *
 * @copyright &copy; 2009 The Open University
 * @author j.platts@open.ac.uk
 * @author Author_Name_2@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package filter/references
 */
require_once(dirname(__FILE__).'/../../config.php');
?>

addonload(ref_filter_init);

var ref_filter_initok = false;

/**
 * Init function
 * Can be called multiple times - will check if the selects it finds have already been treated
 * @return
 */
function ref_filter_init(){
	
	var lsels = new Array();
    //get all ref areas and add to array
	var selsinhere = document.getElementsByTagName("select");
		for(var b=0;b<selsinhere.length;b++){
			if(selsinhere[b].id.search("refselect")!=-1){
				lsels.push(selsinhere[b].id);
				//create a form element for this select/button so data can be submitted
				//ref_filter_makeform(selsinhere[b].id.substr(10));
			}
		}
	if(lsels.length==0){
		return;//no sels found, so return
	}

	// Instantiate and configure YUI Loader:   
	(function() {   
	
	    var loader = new YAHOO.util.YUILoader({   
	        base: <?php 
	            global $CFG;
	            if(isset($CFG->wwwroot)){
	                echo '"'.$CFG->wwwroot.'/lib/yui/"';
	            }else{
	                echo '""';
	            }
	        ?>,
	        //get everything that is required (most should already be loaded)   
	        require: ["get","button","menu","container","connection","dragdrop","event","yahoo-dom-event"],
	        loadOptional: false,   
	        combine: false,  
	        filter: "MIN",   
	        allowRollup: true,
	        skin: {
	        	base: 'assets/skins/',     
	        	defaultSkin: 'sam'
	    	},
	        onSuccess: function() {
	        	ref_filter_initok = true;
	    		//add skin
	    		YAHOO.util.Dom.addClass(document.body, 'yui-skin-sam');
	        }  
	    });   
	  
	// Load the files using the insert() method.   
	loader.insert();
	
	})();
}

function ref_filter_makeform(idnum){
	if(!YAHOO.util.Dom.get('ref_filt_'+idnum)){
		//make form
		var newform = '<form id="ref_filt_'+idnum+'" action="<?php 
	            global $CFG;
	            if(isset($CFG->wwwroot)){
	                echo $CFG->wwwroot.'/filter/references/export_references.php';
	            }
	        ?>" method="post">'+document.getElementById("info_"+idnum).outerHTML+document.getElementById("xml_"+idnum).outerHTML+'</form>';
				document.body.innerHTML+=newform;
	}
}


function ref_filter_onMenuClick(b){
    var idnum = b.id.substr(6);
    
    if(!ref_filter_initok){
    	ref_filter_init();
    }   
    
    var but_selection = YAHOO.util.Dom.get('refselect_'+idnum).value;
    if(but_selection=="null"){
    	return;
    }
    //split value using : 0=name of export, 1=AJAX on or off (if off submit form)
    var optval = but_selection.split(":");
    
    //make a form for this button to submit
    ref_filter_makeform(idnum);
    
    var formObject = YAHOO.util.Dom.get('ref_filt_'+idnum);
    	
    	if(formObject){
    		//add this button selection to form action
    		formObject.action = "<?php 
	            global $CFG;
	            if(isset($CFG->wwwroot)){
	                echo $CFG->wwwroot.'/filter/references/export_references.php';
	            }
	        ?>";
    		formObject.action+="?button="+but_selection;
    	}
    	
    	if(optval[1]=="0"){
    		//submit to page, don't do behind scenes
    		formObject.submit();
    		return false;
    	}
    	//submit form via ajax
		
		YAHOO.util.Connect.setForm(formObject);
		args = formObject;
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '<?php 
	            global $CFG;
	            if(isset($CFG->wwwroot)){
	                echo $CFG->wwwroot.'/filter/references/export_references.php';
	            }
	        ?>'+'?button='+but_selection+'&ajax=true', postcallback);
    	showWait("<?php echo get_string('jsajax','filter_references'); ?>");
    	
    	return false;
    	
}

//callback from ajax request
var args = '';

var responseSuccess = function(o){
    YAHOO.example.container.wait.hide();
    //submit was generally successful - did the export work? Test for ##success##
    if(o.responseText.search("##success##")!=-1){
    	showSuccess();
    }else{
    	//No manually submit, so user can see what is happening
    	args.submit();
    }
};

var responseFailure = function(o){
	YAHOO.example.container.wait.hide();
	//major failure to use ajax, submit manually
	args.submit();
}

var postcallback =
{
  success:responseSuccess,
  failure:responseFailure,
  argument:args
};

YAHOO.namespace("success.container");
function showSuccess(){
    YAHOO.success.container.myPanel = new YAHOO.widget.Panel("myPanel", {
    	width:"400px", 
    	fixedcenter: true, 
    	constraintoviewport: true, 
    	underlay:"matte", 
    	close:true, 
    	visible:true, 
    	draggable:true} );
    YAHOO.success.container.myPanel.setHeader("<?php echo get_string('ajaxsuccess_head','filter_references'); ?>");
    YAHOO.success.container.myPanel.setBody("<?php echo get_string('ajaxsuccess_body','filter_references'); ?>");
    YAHOO.success.container.myPanel.render(document.body);

	var kl = new YAHOO.util.KeyListener(document, { keys:27 },  							
											  { fn:YAHOO.success.container.myPanel.hide,
												scope:YAHOO.success.container.myPanel,
												correctScope:true }, "keyup" );

	YAHOO.success.container.myPanel.cfg.queueProperty("keylisteners", kl);
	kl.enable();
     
}
YAHOO.namespace("example.container");
//Shows a wait dialog while rss feed is loading
function showWait(text){
    YAHOO.example.container.wait =    
             new YAHOO.widget.Panel("wait",     
                 { width:"200px",    
                   fixedcenter:true,    
                   close:false,    
                   draggable:false,    
                   zindex:4,   
                   modal:false,
                   underlay: "none",   
                   visible:false  
                 }    
             );   
       
    YAHOO.example.container.wait.setHeader(text);   
    YAHOO.example.container.wait.render(document.body);  
	YAHOO.example.container.wait.show();
}

function in_array(arr, val){
	var max=arr.length;
	for(var a=0;a<max;a++){
		if(arr[a]==val)return true;
	}
	return false;
}

//define outerhtml for non ie
if (document.body.__defineGetter__) {
       if (HTMLElement) {
              var element = HTMLElement.prototype;
              if (element.__defineGetter__) {
                     element.__defineGetter__("outerHTML",
                           function () {
                                  var parent = this.parentNode;
                                  var el = document.createElement(parent.tagName);
                                  el.appendChild(this);
                                  var shtml = el.innerHTML;
                                  parent.appendChild(this);
                                  return shtml;
                           }
                     );
              }
       }
}